<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>购物车 - 暖心助农选品平台</title>
    <style>
        .quantity-input {
            width: 60px !important;
            text-align: center;
            border-radius: 0;
        }
        .quantity-btn {
            padding: 0.375rem 0.75rem;
            border-radius: 0;
        }
        .quantity-btn:first-child {
            border-top-left-radius: 0.25rem;
            border-bottom-left-radius: 0.25rem;
        }
        .quantity-btn:last-child {
            border-top-right-radius: 0.25rem;
            border-bottom-right-radius: 0.25rem;
        }
    </style>
</head>
<body>
    <div layout:fragment="content">
        <h2>我的购物车</h2>
        
        <div th:if="${cartItems.empty}" class="alert alert-info">
            购物车是空的，快去 <a th:href="@{/}" class="alert-link">选购商品</a> 吧！
        </div>
        
        <div th:unless="${cartItems.empty}">
            <!-- 按农户分组显示商品 -->
            <div th:each="group : ${groupedItems}">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0" th:text="${group.farmer.realName + ' 的商品'}">农户名称</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>商品</th>
                                        <th>单价</th>
                                        <th>数量</th>
                                        <th>小计</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="item : ${group.items}">
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <img th:if="${item.product.image}" th:src="@{${item.product.image}}" class="img-thumbnail me-2" style="width: 50px; height: 50px; object-fit: cover;">
                                                <img th:unless="${item.product.image}" th:src="@{/images/default-product.jpg}" class="img-thumbnail me-2" style="width: 50px; height: 50px; object-fit: cover;">
                                                <span th:text="${item.product.name}">商品名称</span>
                                            </div>
                                        </td>
                                        <td th:text="${'￥' + item.product.price}">单价</td>
                                        <td>
                                            <div class="input-group" style="width: 150px;">
                                                <button class="btn btn-outline-secondary quantity-btn decrease-btn" type="button" th:data-id="${item.id}">-</button>
                                                <input type="number" class="form-control quantity-input" th:value="${item.quantity}" min="1" th:data-id="${item.id}">
                                                <button class="btn btn-outline-secondary quantity-btn increase-btn" type="button" th:data-id="${item.id}">+</button>
                                            </div>
                                        </td>
                                        <td th:text="${'￥' + item.product.price.multiply(new java.math.BigDecimal(item.quantity))}">小计</td>
                                        <td>
                                            <button class="btn btn-danger btn-sm delete-btn" th:data-id="${item.id}">删除</button>
                                        </td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="3" class="text-end">农户商品小计：</td>
                                        <td th:text="${'￥' + group.subtotal}">小计金额</td>
                                        <td></td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 总计和结算按钮 -->
            <div class="card">
                <div class="card-body">
                    <form th:action="@{/cart/checkout}" method="post" id="checkoutForm">
                        <!-- 收货信息 -->
                        <div class="mb-4">
                            <h5>收货信息</h5>
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="contactName">收货人姓名</label>
                                        <input type="text" class="form-control" id="contactName" name="contactName" 
                                               th:value="${currentUser.realName}" required>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="contactPhone">联系电话</label>
                                        <input type="tel" class="form-control" id="contactPhone" name="contactPhone" 
                                               th:value="${currentUser.phone}" required>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label for="deliveryAddress">收货地址</label>
                                        <input type="text" class="form-control" id="deliveryAddress" name="deliveryAddress" 
                                               th:value="${currentUser.address}" required>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 总计金额和提交按钮 -->
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <span class="h5">总计：</span>
                                <span class="h5 text-danger" th:text="${'￥' + total}">总金额</span>
                                <span class="ms-3">当前余额：</span>
                                <span th:text="${'￥' + currentUser.balance}" th:data-balance="${currentUser.balance}">余额</span>
                            </div>
                            <button type="submit" class="btn btn-primary" id="checkoutButton">去结算</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <th:block layout:fragment="scripts">
        <script th:inline="javascript">
            $(document).ready(function() {
                var contextPath = /*[[@{/}]]*/ '';
                var total = /*[[${total}]]*/ 0;
                var balance = /*[[${currentUser.balance}]]*/ 0;
                
                // 增加数量
                $('.increase-btn').click(function() {
                    var id = $(this).data('id');
                    var input = $('.quantity-input[data-id="' + id + '"]');
                    var quantity = parseInt(input.val()) + 1;
                    updateQuantity(id, quantity);
                });
                
                // 减少数量
                $('.decrease-btn').click(function() {
                    var id = $(this).data('id');
                    var input = $('.quantity-input[data-id="' + id + '"]');
                    var quantity = parseInt(input.val()) - 1;
                    if (quantity >= 1) {
                        updateQuantity(id, quantity);
                    }
                });
                
                // 直接修改数量
                $('.quantity-input').change(function() {
                    var id = $(this).data('id');
                    var quantity = parseInt($(this).val());
                    if (quantity >= 1) {
                        updateQuantity(id, quantity);
                    } else {
                        $(this).val(1);
                        updateQuantity(id, 1);
                    }
                });
                
                // 删除商品
                $('.delete-btn').click(function() {
                    var id = $(this).data('id');
                    if (confirm('确定要删除这个商品吗？')) {
                        $.ajax({
                            url: contextPath + 'cart/remove/' + id,
                            type: 'POST',
                            success: function(response) {
                                if (response === 'success') {
                                    location.reload();
                                } else {
                                    alert('删除失败：' + response);
                                }
                            },
                            error: function() {
                                alert('删除失败，请稍后重试');
                            }
                        });
                    }
                });
                
                // 更新数量的函数
                function updateQuantity(id, quantity) {
                    $.post(contextPath + 'cart/update/' + id, { quantity: quantity }, function() {
                        location.reload();
                    });
                }

                // 检查余额并处理结算
                $('#checkoutForm').on('submit', function(e) {
                    if (total > balance) {
                        e.preventDefault();
                        alert('余额不足！您的余额为￥' + balance + '，订单总额为￥' + total + '，请先充值。');
                        return false;
                    }
                });
            });
        </script>
    </th:block>
</body>
</html> 